package com.weiaibenpao.demo.chislim;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.weiaibenpao.demo.chislim.sport.utils.ShareUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/14.
 */

public class RunningResultDialog extends Dialog {
    @BindView(R.id.content)
    RelativeLayout content;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.tv_calorie)
    TextView tvCalorie;
    @BindView(R.id.tv_running_time)
    TextView tvRunningTime;
    @BindView(R.id.tv_slogan)
    TextView tvSlogan;
    private Context mContext;
    private String mDistance;
    private String mCalorie;
    private int mTime;


    public RunningResultDialog(@NonNull Context context, String distance, String calorie, int time) {
        super(context, R.style.style_primary_dialog);
        mContext = context;
        mDistance = distance;
        mCalorie = calorie;
        mTime = time;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_running_result);
        ButterKnife.bind(this);
        tvCalorie.setText(mCalorie);
        tvDistance.setText(mDistance);
        tvRunningTime.setText(RunningUtils.getHmsTime(mTime));
    }


    @OnClick({R.id.tv_wechat, R.id.tv_moment, R.id.tv_qq, R.id.tv_qzone, R.id.tv_sina})
    public void onClick(View view) {
        ShareUtils shareUtils = new ShareUtils(mContext);//优化点：getViewBitmap可以在这之前调用一次
        switch (view.getId()) {
            case R.id.tv_wechat:
                shareUtils.shareBitmap(getViewBitmap(content), SHARE_MEDIA.WEIXIN, mDistance);
                break;

            case R.id.tv_moment:
                shareUtils.shareBitmap(getViewBitmap(content), SHARE_MEDIA.WEIXIN_CIRCLE, mDistance);
                break;

            case R.id.tv_qq:
                shareUtils.shareBitmap(getViewBitmap(content), SHARE_MEDIA.QQ, mDistance);
                break;

            case R.id.tv_qzone:
                shareUtils.shareBitmap(getViewBitmap(content), SHARE_MEDIA.QZONE, mDistance);
                break;

            case R.id.tv_sina:
                shareUtils.shareBitmap(getViewBitmap(content), SHARE_MEDIA.SINA, mDistance);
                break;
        }

    }


    public Bitmap getViewBitmap(View v) {
        tvSlogan.setVisibility(View.VISIBLE);

        v.clearFocus();
        v.setPressed(false);
        boolean willNotCache = v.willNotCacheDrawing();
        v.setWillNotCacheDrawing(false);
        // Reset the drawing cache background color to fully transparent
        // for the duration of this operation
        int color = v.getDrawingCacheBackgroundColor();
        v.setDrawingCacheBackgroundColor(0);
        if (color != 0) {
            v.destroyDrawingCache();//若想更新cache, 必须要调用destroyDrawingCache方法把旧的cache销毁，才能建立新的。
        }
        v.buildDrawingCache();//If you call buildDrawingCache() manually without calling setDrawingCacheEnabled(true), you should cleanup the cache by calling destroyDrawingCache() afterwards.
        Bitmap cacheBitmap = v.getDrawingCache();
        if (cacheBitmap == null) {
            Log.e("Folder", "failed getViewBitmap(" + v + ")", new RuntimeException());
            return null;
        }
        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);//创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
        // Restore the view
        v.destroyDrawingCache();
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(color);

        tvSlogan.setVisibility(View.INVISIBLE);
        return bitmap;
    }


}
